/* 
 * Copyright 2012 Mikaël GUILLEMOT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.g2de.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import org.g2de.BasicGameFrame;
import org.g2de.display.Activable;
import org.g2de.display.MouseControlListener;

/**
 * Ui pour le deplacement, la fermeture et la reduction de la fenetre
 * 
 * @author Mikael GUILLEMOT
 * @version 1.0
 * @since
 * 
 */

// TODO rendre la barre transparente quant on est pas dessu, desactiver le drag
// si on est en fullscreen, sur android ou dans une aplete
public class UIBasicOperations extends AbstractUI implements Activable {

	private static final int BTSIZE = 16;

	private UIPicBoutton btclose = null;
	private UIPicBoutton btmin = null;
	private Point dragOrigine = null;
	private Point WindowOrigine = null;
	private boolean isdraging = false;
	private String title = null;

	private BufferedImage BTclose = null;
	private BufferedImage BTcloseOver = null;
	private BufferedImage BTminus = null;
	private BufferedImage BTminusOver = null;

	public UIBasicOperations(String p_title) {
		super();
		generatePicture();
		btclose = new UIPicBoutton(BTclose, BTcloseOver);
		btmin = new UIPicBoutton(BTminus, BTminusOver);
		title = p_title;
		this.setSize(BasicGameFrame.X_SIZE - 1, BTclose.getHeight());
		int offsetClose = BasicGameFrame.X_SIZE - 1 - BTclose.getWidth();
		int offsetMin = offsetClose - BTminus.getWidth();
		btclose.setOrigin(offsetClose, 0);
		btmin.setOrigin(offsetMin, 0);
		btclose.setVisible(true);
		btmin.setVisible(true);
		btclose.addActionListener(this);
		btmin.addActionListener(this);
		this.add(btclose);
		this.add(btmin);
	}

	/**
	 * set the title of basic ui operation bar
	 * 
	 * @param title
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	protected void selfBehavior(MouseEvent arg0, short action) {
		if (action == MouseControlListener.EV_PRESSED) {
			dragOrigine = new Point(arg0.getXOnScreen(), arg0.getYOnScreen());
			WindowOrigine = BasicGameFrame.getInstance().getWindowLocation();
			isdraging = true;
		} else if (action == MouseControlListener.EV_DRAG && isdraging) {
			int x = arg0.getXOnScreen() - dragOrigine.x;
			int y = arg0.getYOnScreen() - dragOrigine.y;
			BasicGameFrame.getInstance().moveWindow(WindowOrigine.x + x, WindowOrigine.y + y);
		} else
			isdraging = false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see gameEngine.coreEngine.Renderable#display(java.awt.Graphics2D)
	 */
	@Override
	public void selfRender(Graphics2D buffer) {
		buffer.setColor(new Color(0xF0F0F0FF, true));
		buffer.fillRect(activeZone.x, activeZone.y, activeZone.width, activeZone.height);
		buffer.setColor(Color.black);
		buffer.drawRect(activeZone.x, activeZone.y, activeZone.width, activeZone.height);
		buffer.drawString(title, (this.activeZone.width - title.length() * 10) / 2, 12);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see gameEngine.coreEngine.Activable#actionPerformed(int, int)
	 */
	@Override
	public void actionPerformed(int objectId, int actionId) {
		if (actionId == MouseControlListener.EV_CLICK)
			if (objectId == btclose.hashCode()) {
				BasicGameFrame.getInstance().stop();
			} else if (objectId == btmin.hashCode()) {
				BasicGameFrame.getInstance().minimize();
			}
	}

	// genere les icones de base pour l'ui
	private void generatePicture() {
		BufferedImage tmp = new BufferedImage(BTSIZE, BTSIZE, BufferedImage.TYPE_3BYTE_BGR);
		Graphics g = tmp.getGraphics();
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0, 0, BTSIZE, BTSIZE);
		g.setColor(Color.BLACK);
		g.drawLine(0 + 2, 0 + 2, BTSIZE - 2, BTSIZE - 2);
		g.drawLine(BTSIZE - 2, 0 + 2, 0 + 2, BTSIZE - 2);
		g.drawRect(0, 0, BTSIZE, BTSIZE);
		BTclose = tmp;
		tmp = new BufferedImage(BTSIZE, BTSIZE, BufferedImage.TYPE_3BYTE_BGR);
		g = tmp.getGraphics();
		g.setColor(Color.GRAY);
		g.fillRect(0, 0, BTSIZE, BTSIZE);
		g.setColor(Color.BLACK);
		g.drawLine(0 + 2, 0 + 2, BTSIZE - 2, BTSIZE - 2);
		g.drawLine(BTSIZE - 2, 0 + 2, 0 + 2, BTSIZE - 2);
		g.drawRect(0, 0, BTSIZE, BTSIZE);
		BTcloseOver = tmp;

		tmp = new BufferedImage(BTSIZE, BTSIZE, BufferedImage.TYPE_3BYTE_BGR);
		g = tmp.getGraphics();
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0, 0, BTSIZE, BTSIZE);
		g.setColor(Color.BLACK);
		g.drawLine(0 + 2, BTSIZE / 2, BTSIZE - 2, BTSIZE / 2);
		g.drawRect(0, 0, BTSIZE, BTSIZE);
		BTminus = tmp;
		tmp = new BufferedImage(BTSIZE, BTSIZE, BufferedImage.TYPE_3BYTE_BGR);
		g = tmp.getGraphics();
		g.setColor(Color.GRAY);
		g.fillRect(0, 0, BTSIZE, BTSIZE);
		g.setColor(Color.BLACK);
		g.drawLine(0 + 2, BTSIZE / 2, BTSIZE - 2, BTSIZE / 2);
		g.drawRect(0, 0, BTSIZE, BTSIZE);
		BTminusOver = tmp;

	}
}
